<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%
    // 从session中获取错误信息和医生信息
    String addError = (String) session.getAttribute("addError");
    Object addDoctor = session.getAttribute("addDoctor");
    
    // 如果有错误信息，添加到request中并从session中移除
    if (addError != null) {
        request.setAttribute("error", addError);
        session.removeAttribute("addError");
    }
    
    // 如果有医生信息且request中没有医生信息，添加到request中并从session中移除
    if (addDoctor != null && request.getAttribute("doctor") == null) {
        request.setAttribute("doctor", addDoctor);
        session.removeAttribute("addDoctor");
    }
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <base href="<%=request.getContextPath()%>/">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>医生管理 - 医疗预约系统后台</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link rel="stylesheet" href="statics/css/hisStyle.css">
    <link rel="stylesheet" href="statics/css/common-styles.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<!-- 侧边栏 -->
<%@include file="../common/sidebar.jsp"%>

<!-- 主内容区域 -->
<div class="main-content">
    <!-- 顶部导航 -->
    <%@include file="../common/top.jsp"%>

    <!-- 内容区域 -->
    <div class="content">
        <div class="function-page">
            <div class="page-header">
                <h1>医生管理</h1>
            </div>
            
            <div class="action-bar">
                <button id="addDoctorBtn" class="btn btn-primary">
                    <i class="fas fa-plus-circle"></i> 新增医生
                </button>
            </div>

            <!-- 查询表单 -->
            <div class="search-filter">
                <form action="doctor/find" method="post" class="search-form">
                    <div class="form-group">
                        <label for="doctorName">医生姓名：</label>
                        <input type="text" id="doctorName" name="doctorName" 
                            value="${condition.doctorName}" placeholder="请输入医生姓名" autocomplete="off" class="form-control">
                    </div>
                    
                    <div class="form-group">
                        <label for="deptId">所属科室：</label>
                        <select id="deptId" name="deptId" class="form-control">
                            <option value="">全部</option>
                        <c:forEach items="${departmentList}" var="dept">
                            <option value="${dept.deptId}" <c:if test="${condition.deptId eq dept.deptId}">selected</c:if>>${dept.deptName}</option>
                        </c:forEach>
                        </select>
                    </div>
                    
                    <div class="form-group">
                        <label for="hospitalId">所属医院：</label>
                        <select id="hospitalId" name="hospitalId" class="form-control">
                            <option value="">全部</option>
                        <c:forEach items="${hospitalList}" var="hospital">
                            <option value="${hospital.hospitalId}" <c:if test="${condition.hospitalId eq hospital.hospitalId}">selected</c:if>>${hospital.hospitalName}</option>
                        </c:forEach>
                        </select>
                    </div>
            
            <div>
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-search"></i> 查询
                </button>
            </div>
        </form>
        <span class="search-info">总计 ${pageInfo.total} 个医生</span>
    </div>

    <div class="data-section">
                <table class="data-table">
                    <thead>
                        <tr>
                            <th>医生ID</th>
                            <th>医生姓名</th>
                            <th>性别</th>
                            <th>电话</th>
                            <th>身份证号</th>
                            <th>所属科室</th>
                            <th>所属医院</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <c:forEach items="${pageInfo.list}" var="doctor">
                            <tr>
                                <td>${doctor.doctorId}</td>
                                <td>${doctor.doctorName}</td>
                                <td>${doctor.gender}</td>
                                <td>${doctor.phone}</td>
                                <td>${doctor.idCard}</td>
                                <td>
                                    <c:choose>
                                        <c:when test="${not empty doctor.deptId}">
                                            <c:forEach var="dept" items="${departmentList}">
                                                <c:if test="${dept.deptId eq doctor.deptId}">${dept.deptName}</c:if>
                                            </c:forEach>
                                        </c:when>
                                        <c:otherwise>未分配</c:otherwise>
                                    </c:choose>
                                </td>
                                <td>
                                    <c:choose>
                                        <c:when test="${not empty doctor.hospitalName}">${doctor.hospitalName}</c:when>
                                        <c:otherwise>未分配</c:otherwise>
                                    </c:choose>
                                </td>
                                <td class="operations">
                                    <button class="btn btn-secondary btn-sm edit-btn" data-id="${doctor.doctorId}">
                                        <i class="fas fa-edit"></i> 编辑
                                    </button>
                                    <a href="doctor/removeById?doctorId=${doctor.doctorId}" class="btn btn-danger btn-sm delete-btn"
                                       onclick="return confirm('确定删除该医生吗？删除前请确保该医生没有排班！')">
                                        <i class="fas fa-trash-alt"></i> 删除
                                    </a>
                                </td>
                            </tr>
                        </c:forEach>
                    </tbody>
                </table>
            </div>
        
        <!-- 分页控件 -->
        <div class="pagination-container">
            <div class="pagination">
                <c:if test="${pageInfo.hasPreviousPage}">
                    <a href="${pageContext.request.contextPath}/doctor/find?pageNum=1&doctorName=${condition.doctorName}&deptId=${condition.deptId}&hospitalId=${condition.hospitalId}" class="pagination-link">
                        <i class="fas fa-angle-double-left"></i> 首页
                    </a>
                    <a href="${pageContext.request.contextPath}/doctor/find?pageNum=${pageInfo.prePage}&doctorName=${condition.doctorName}&deptId=${condition.deptId}&hospitalId=${condition.hospitalId}" class="pagination-link">
                        <i class="fas fa-angle-left"></i> 上一页
                    </a>
                </c:if>
                
                <c:forEach items="${pageInfo.navigatepageNums}" var="pageNum">
                    <c:if test="${pageNum == pageInfo.pageNum}">
                        <a href="${pageContext.request.contextPath}/doctor/find?pageNum=${pageNum}&doctorName=${condition.doctorName}&deptId=${condition.deptId}&hospitalId=${condition.hospitalId}" class="pagination-link active">${pageNum}</a>
                    </c:if>
                    <c:if test="${pageNum != pageInfo.pageNum}">
                        <a href="${pageContext.request.contextPath}/doctor/find?pageNum=${pageNum}&doctorName=${condition.doctorName}&deptId=${condition.deptId}&hospitalId=${condition.hospitalId}" class="pagination-link">${pageNum}</a>
                    </c:if>
                </c:forEach>
                
                <c:if test="${pageInfo.hasNextPage}">
                    <a href="${pageContext.request.contextPath}/doctor/find?pageNum=${pageInfo.nextPage}&doctorName=${condition.doctorName}&deptId=${condition.deptId}&hospitalId=${condition.hospitalId}" class="pagination-link">
                        <i class="fas fa-angle-right"></i> 下一页
                    </a>
                    <a href="${pageContext.request.contextPath}/doctor/find?pageNum=${pageInfo.pages}&doctorName=${condition.doctorName}&deptId=${condition.deptId}&hospitalId=${condition.hospitalId}" class="pagination-link">
                        <i class="fas fa-angle-double-right"></i> 末页
                    </a>
                </c:if>
            </div>
        </div>
        <span class="pagination-info">共 ${pageInfo.total} 条记录，当前第 ${pageInfo.pageNum} / ${pageInfo.pages} 页</span>
        </div>

    <!-- 医生信息模态框 -->
    <div id="doctorModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h3 id="doctorModalTitle">医生信息</h3>
                <span class="close">&times;</span>
            </div>
            
            <c:if test="${not empty error}">
                <div class="error-message">${error}</div>
            </c:if>

            <form id="doctorForm" method="post">
                <script>
                    // 根据是否有doctorId设置表单提交地址
                    document.getElementById('doctorForm').action = '${empty doctor.doctorId ? "${pageContext.request.contextPath}/doctor/add" : "${pageContext.request.contextPath}/doctor/modify"}';
                </script>
                <input type="hidden" id="doctorId" name="doctorId" value="${doctor.doctorId}">

                <div class="form-group">
                    <label for="form-doctorName">医生姓名：</label>
                    <input type="text" id="form-doctorName" name="doctorName" value="${doctor.doctorName}" required placeholder="请输入医生姓名" class="form-control">
                </div>
                <div class="form-group">
                    <label for="form-gender">性别：</label>
                    <select id="form-gender" name="gender" required class="form-control">
                        <option value="男" <c:if test="${doctor.gender eq '男'}">selected</c:if>>男</option>
                        <option value="女" <c:if test="${doctor.gender eq '女'}">selected</c:if>>女</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="form-phone">电话：</label>
                    <input type="tel" id="form-phone" name="phone" value="${doctor.phone}" required placeholder="请输入电话号码" class="form-control">
                </div>
                <div class="form-group">
                    <label for="form-idCard">身份证号：</label>
                    <input type="text" id="form-idCard" name="idCard" value="${doctor.idCard}" required placeholder="请输入身份证号" class="form-control">
                </div>
                <div class="form-group">
                    <label for="form-deptId">所属科室：</label>
                    <select id="form-deptId" name="deptId" required class="form-control">
                        <option value="">--请选择科室--</option>
                        <c:forEach items="${departmentList}" var="dept">
                            <option value="${dept.deptId}" <c:if test="${doctor.deptId eq dept.deptId}">selected</c:if>>${dept.deptName}</option>
                        </c:forEach>
                    </select>
                </div>
                <div class="form-group">
                    <label for="form-email">邮箱：</label>
                    <input type="email" id="form-email" name="email" value="${doctor.email}" placeholder="请输入邮箱地址（选填）" class="form-control">
                </div>
                <div class="form-group">
                    <label for="form-hospitalId">所属医院：</label>
                    <select id="form-hospitalId" name="hospitalId" required class="form-control">
                        <option value="">--请选择医院--</option>
                        <c:forEach items="${hospitalList}" var="hospital">
                            <option value="${hospital.hospitalId}" <c:if test="${doctor.hospitalId eq hospital.hospitalId}">selected</c:if>>${hospital.hospitalName}</option>
                        </c:forEach>
                    </select>
                </div>
                <div class="form-actions">
                      <button type="button" id="cancelBtn" class="btn btn-default">取消</button>
                      <button type="submit" class="btn btn-primary">${empty doctor.doctorId ? '保存' : '保存修改'}</button>
                  </div>
            </form>
        </div>
    </div>


</div>
</div>
</div>
    <script>
        // 为侧边栏的医生管理项添加激活状态
        document.addEventListener('DOMContentLoaded', function() {
            const menuItems = document.querySelectorAll('.menu-item');
            menuItems.forEach(item => {
                if (item.getAttribute('href') === 'doctor/find' || item.getAttribute('href') === '${pageContext.request.contextPath}/doctor/find') {
                    item.classList.add('active');
                } else {
                    item.classList.remove('active');
                }
            });
            
            // 新增医生按钮点击事件
            document.getElementById('addDoctorBtn').addEventListener('click', function() {
                // 清空表单内容
                document.getElementById('doctorId').value = '';
                document.getElementById('form-doctorName').value = '';
                document.getElementById('form-phone').value = '';
                document.getElementById('form-idCard').value = '';
                document.getElementById('form-email').value = '';
                document.getElementById('form-deptId').value = '';
                document.getElementById('form-hospitalId').value = '';
                document.getElementById('form-gender').value = '男';
                
                // 设置表单提交地址为新增
                document.getElementById('doctorForm').action = '${pageContext.request.contextPath}/doctor/add';
                
                // 设置模态框标题并显示
                document.getElementById('doctorModalTitle').textContent = '添加医生';
                document.getElementById('doctorModal').style.display = 'block';
            });
            
            // 编辑按钮点击事件
            document.querySelectorAll('.edit-btn').forEach(btn => {
                btn.addEventListener('click', function() {
                    const doctorId = this.getAttribute('data-id');
                    // 使用完整的contextPath构建URL，避免路径解析问题
                    window.location.href = '${pageContext.request.contextPath}/doctor/findById?doctorId=' + doctorId;
                });
            });
            
            // 关闭模态框
            document.querySelectorAll('.close, #cancelBtn').forEach(btn => {
                btn.addEventListener('click', function() {
                    document.getElementById('doctorModal').style.display = 'none';
                });
            });
            
            // 点击模态框外部关闭
            window.addEventListener('click', function(event) {
                const modal = document.getElementById('doctorModal');
                if (event.target == modal) {
                    modal.style.display = 'none';
                }
            });
            
            // 检查是否有编辑参数或错误信息，如果有则显示模态框
            if (${!empty doctor.doctorId || !empty error}) {
                document.getElementById('doctorModal').style.display = 'block';
                // 设置模态框标题
                document.getElementById('doctorModalTitle').textContent = '${empty doctor.doctorId ? "添加医生" : "编辑医生"}';
            }
        });
    </script>
</body>
</html>